package com.game_info.controller;

import java.util.List;

import com.game_info.service.GamesInfoService;
import com.game_info.utils.AjaxResult;
import com.game_info.vo.GamesInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * games_info表Controller
 *
 * @author ttj
 * @date 2020-04-14
 */
@Controller
@RequestMapping("/info")
public class GamesInfoController {

    @Autowired
    private GamesInfoService gamesInfoService;


    /**
     * 查询列表
     */
    @PostMapping("/list.do")
    @ResponseBody
    public AjaxResult list(@RequestBody GamesInfo gamesInfo) {
        List<GamesInfo> list = gamesInfoService.selectGamesInfoList(gamesInfo);
        return AjaxResult.put(list, "查询文章列表方法执行成功", list.size());
    }

    /**
     * 根据文章id查询文章单个信息
     * @param infoId
     * @return
     */
    @GetMapping("/selectGameInfoById.do")
    @ResponseBody
    public GamesInfo selectGameInfoById(Integer infoId){
        return gamesInfoService.selectGamesInfoById(infoId);
    }

    /**
     * 分页查询(固定页数)
     *
     * @param page 需要查询的页数
     * @return
     */
    @GetMapping("/selectGamesInfoListLimit.do")
    @ResponseBody
    public AjaxResult selectGamesInfoListLimit(Integer page, @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = 5;//默认分页数据量5
        }
        //入参小于0的情况.
        if (page <= 0) {
            page = 1;
        }
        Integer max=gamesInfoService.selectListCount();//总数
        List<GamesInfo> list=gamesInfoService.selectGamesInfoListLimit((page - 1) * limit, limit);
        return AjaxResult.put(list,"分页查询方法执行成功",max);
    }

    /**
     * 根据关键词模糊查询全部字段信息
     * @param keyword
     * @return
     */
    @GetMapping("/selectLikeKeyword.do")
    @ResponseBody
    public List<GamesInfo> selectLikeKeyword(String keyword){
        return gamesInfoService.selectLikeKeyword(keyword);
    }

    /**
     * 获取文章总数
     * @return
     */
    @GetMapping("/selectListCount.do")
    @ResponseBody
    public Integer selectListCount(){
        return gamesInfoService.selectListCount();
    }

    /**
     * 新增保存
     */
    @PostMapping("/add.do")
    @ResponseBody
    public AjaxResult add(@RequestBody GamesInfo gamesInfo) {
        return AjaxResult.put(gamesInfoService.insertGamesInfo(gamesInfo), "新增文章方法执行成功");
    }


    /**
     * 修改保存
     */
    @PostMapping("/edit.do")
    @ResponseBody
    public AjaxResult edit(@RequestBody GamesInfo gamesInfo) {
        return AjaxResult.put(gamesInfoService.updateGamesInfo(gamesInfo), "修改文章方法执行成功");
    }

    /**
     * 删除
     *
     */
    @PostMapping("/remove.do")
    @ResponseBody
    public AjaxResult remove(@RequestBody Integer infoId) {
        return AjaxResult.put(gamesInfoService.deleteGamesInfoById(infoId), "执行单个删除方法成功");
    }
}
